﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Business.Data;
using NHibernate.Transform;
using Newtonsoft.Json;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.SystemManage.Basics.RightConfig.UserRight
{
    public partial class UserRightAssign : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected string CurrentUser { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);
            if(Request["ajax"] == "1")
            {
                string user = Request["user"];
                string value = Request["val"];
                string group = Request["group"];
                IList<string> codes = JsonConvert.DeserializeObject<IList<string>>(Request["codes"]);
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    //删除权限
                    if (value == "0")
                    {
                        session.CreateQuery("delete from UrUserRight where GroupCode=:GroupCode and UserCode=:UserCode and RightCode in (:RightCodes)")
                            .SetParameter("GroupCode", group)
                            .SetParameter("UserCode", user)
                            .SetParameterList("RightCodes", codes.ToArray())
                            .ExecuteUpdate();
                    }
                    //增加权限
                    else
                    {
                        foreach (string code in codes)
                        {
                            int count = Convert.ToInt32(session.CreateQuery("select count(*) from UrUserRight where GroupCode=:GroupCode and UserCode=:UserCode and RightCode=:RightCode")
                                .SetParameter("GroupCode", group)
                                .SetParameter("UserCode", user)
                                .SetParameter("RightCode", code)
                                .UniqueResult());
                            if (count == 0)
                            {
                                UrUserRight userRight = new UrUserRight();
                                userRight.GroupCode= group;
                                userRight.RightCode = code;
                                userRight.RightValue = 1;
                                userRight.UserCode = user;
                                session.Save(userRight);
                            }
                        }
                    }
                    session.Flush();
                }
                Response.Write("{\"val\":" + value + "}");
                return;
            }


            using(ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                CurrentUser = Request["user"];

                roles.DataSource = session.CreateQuery("from SysUserInfo").List();
                roles.DataBind();


                group.DataSource = session.CreateQuery("from UrConfig where ItemGroup='RightGroup'").List();
                group.DataTextField = "ItemValue";
                group.DataValueField = "ItemName";
                group.DataBind();

                string groupValue = Request["group"];
                if (!string.IsNullOrEmpty(groupValue))
                {
                    group.SelectedValue = groupValue;
                }

                string sql = @"select b.right_value as RightValue,a.right_code as RightCode,a.right_name as RightName
                            from ur_right
 a left outer join view_user_right b on 
                            (a.group_code=b.group_code and a.right_code=b.right_code and b.user_code=:code)where a.group_code=:group order by a.right_code";

                rights.DataSource = session.CreateSQLQuery(sql).
                    SetParameter("code", CurrentUser).
                    SetParameter("group", group.SelectedValue).
                    SetResultTransformer(Transformers.AliasToEntityMap).List();
                rights.DataBind();
            }
        }
    }
}
